{extend name="shared/layout" /}
{block name="title"}新增图片{/block}
{block name="css"}
    <style>
        .upfile{position:relative;overflow:hidden;cursor:pointer;margin-bottom:0;padding:6px 8px;}
        .upfile i{padding-right:3px !important;}
        .file{ position:absolute;right:0;top:0;width:100px;height:100px;opacity:0; ilter:alpha(opacity=0);cursor:pointer;margin-bottom: 0px;margin-right: 0px; }
        .previewImg{display:none;border:1px solid #37a3ed;}
    </style>
{/block}
{block name="javascript"}
    <script type="text/javascript">
        $(document).ready(function () {
            $("#ok").bind("click", Save);
            $("#cancel").bind("click", function () {
                parent.layer.close(parent.layer_add_index);
            });
        })

        var fileEls = document.getElementById('file');        
        var fullBase64;
        var base64;
        $(".file").change(function(evt) {
            if (!window.FileReader) return;
            var files = evt.target.files;
            var suffix = $(this).attr("suffix");

            for (var i = 0, f; f = files[i]; i++) {  
                if (!f.type.match('image.*')) {  
                    continue;
                }
                var imgagetype = files[i].type;
                var reader = new FileReader();
                reader.onload = (function(theFile) {
                    return function(e) {
                        fullBase64 = e.target.result;
                        appendFile(fullBase64,imgagetype,suffix);
                    };
                })(f);
                reader.readAsDataURL(f);
            }
        });

        function appendFile(entry,imgagetype,suffix) {
            var img = new Image();
            img.src = entry;
            
            img.onload = function () {
                var that = this;
                /*图片不压缩*/
                var w = that.width,h = that.height;                                

                //生成canvas
                var canvas = document.createElement('canvas');
                var ctx = canvas.getContext('2d');
                $(canvas).attr({width : w, height : h});
                ctx.drawImage(that, 0, 0, w, h);
                base64 = canvas.toDataURL(imgagetype, 1);
                document.getElementById('previewImg_'+suffix).src = base64;
                document.getElementById('image_'+suffix).value = base64;
            }
        }
    
        function Save() {
            layui.use(['layer'], function(){
                var layer = layui.layer;

                var image_name = $.trim($("input[name=image_name]").val());
                if (image_name.length == 0) {
                    layer.tips('图片名称不能为空！', 'input[name=image_name]', { tips: [2, '#78BA32'] });
                    return false;
                }
                
                var image_url = $.trim($("input[name=image_url]").val());
                var image_cover1 = $.trim($('#image_cover1').val());
                if (image_url.length == 0 && image_cover1.length == 0) {
                    layer.tips('请上传图片！', 'input[name=image_url]', { tips: [2, '#78BA32'] });
                    return false;
                }
                
                var load_index = layer.load(0, { shade: false });
                $.ajax({
                    type: "post",
                    url: "{:url('image/add')}?t=" + new Date(),
                    data: $("form").serialize(),
                    dataType: "json",
                    success: function (result) {
                        layer.close(load_index);
                        if (result.code == 0) {
                            layer.alert('保存成功！', function () {
                                if (typeof (parent.initTable) == "function")
                                    parent.initTable();
                                parent.layer.close(parent.layer_add_index)
                            });
                        } else {
                            layer.alert(result.msg);
                        }
                    }
                });
            });
        }
    </script>
{/block}
{block name="content"}
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-content">
                        <form method="post" id="fmNew" class="form-horizontal">                                       
                            <div class="form-group">
                                <label class="col-sm-2 control-label">图片名称：</label>
                                <div class="col-sm-5">
                                    <input type="text" class="form-control" name="image_name" placeholder="请输入图片名称" />
                                </div>
                            </div>                            
                            <div class="form-group">
                                <label class="col-sm-2 control-label">图片：</label>
                                <div class="col-sm-6">
                                    <input type="text" class="form-control" name="image_url" placeholder="图片地址(上传或者直接填写外链)" />
                                </div>
                                <div class="col-sm-1">
                                    <span id="btn-upfile" class="btn btn-warning btn-sm" style="margin-bottom:0;padding:6px 8px;">
                                        <i class="glyphicon glyphicon-folder-open" style="padding-right:3px;"></i> 选择上传<input type="hidden" name="image_cover1" id="image_cover1" /><input id="file" class="file" type="file" name="img" suffix="cover1" />
                                    </span>
                                </div>
                                <div class="col-sm-1">
                                    <img id="previewImg_cover1" style="height:34px;">
                                </div>
                            </div>                            
                            <div class="hr-line-dashed"></div>
                            <div class="form-group">
                                <div class="col-sm-12 col-sm-offset-5">
                                    <button class="btn btn-primary" id="ok" type="button">提交</button>
                                    <button class="btn btn-white" id="cancel" type="button">取消</button>
                                </div>
                            </div>
                        </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>        
{/block}